
#include "ServerDescriptor.h"
#include "DebugStream.h"

namespace Engine {

CServerDescriptor::CServerDescriptor(string& desc) 
{
	int pos1 = 0;
	int pos2 = 0;

	if(desc.find(SERVER_PREFIX_FORWARD) == 0) {
		mType = SERVER_TYPE_FORWARD;
		pos1 = string(SERVER_PREFIX_FORWARD).length();
	}
	else
	if(desc.find(SERVER_PREFIX_BACKWARD) == 0) {
		mType = SERVER_TYPE_BACKWARD;
		pos1 = string(SERVER_PREFIX_BACKWARD).length();
	}
	else
	if(desc.find(SERVER_PREFIX_PROXY) == 0) {
		mType = SERVER_TYPE_PROXY;
		pos1 = string(SERVER_PREFIX_PROXY).length();
	}
	else {
		mType = SERVER_TYPE_UNKNOWN;
		return;
	}

	pos2 = desc.find(':', pos1);
	if(pos2 <= 0) {
		mType = SERVER_TYPE_UNKNOWN;
		return;
	}

	mIp = desc.substr(pos1, pos2-pos1);

	pos1 = pos2+1;
	pos2 = desc.find('?', pos1);

	if(pos2 <= 0) {
		mPort = boost::lexical_cast<int>(desc.substr(pos1));
		return;
	}

	mPort = boost::lexical_cast<int>(desc.substr(pos1, pos2-pos1));
	
	string name;
	string value;

	while(1) {
		pos1 = pos2+1;
		pos2 = desc.find('=', pos1);
		if(pos2 <= 0) break;
		name = desc.substr(pos1, pos2-pos1);

		pos1 = pos2+1;
		pos2 = desc.find('$', pos1);
		if(pos2 <= 0) {
			value = desc.substr(pos1);
			mParams[name] = value;
			break;
		}
		else {
			value = desc.substr(pos1, pos2-pos1);
			mParams[name] = value;
		}
	}
	
}
}